package cn.ibdsr.human.controller;

import cn.ibdsr.human.constant.BizConstants;
import cn.ibdsr.human.constant.Const;
import cn.ibdsr.human.constant.Result;
import cn.ibdsr.human.db.mapper.UserMapper;
import cn.ibdsr.human.db.model.Company;
import cn.ibdsr.human.db.model.Marry;
import cn.ibdsr.human.db.model.User;
import cn.ibdsr.human.exception.BizExceptionEnum;
import cn.ibdsr.human.exception.BusinessException;
import cn.ibdsr.human.manager.LogFormatManager;
import cn.ibdsr.human.service.JobService;
import cn.ibdsr.human.service.UserDetailService;
import cn.ibdsr.human.transfer.*;
import cn.ibdsr.human.transfer.job.JobCompanyResponseDTO;
import cn.ibdsr.human.transfer.job.JobListResponseDTO;
import cn.ibdsr.human.transfer.job.JobQueryDTO;
import cn.ibdsr.human.transfer.job.JobUserResponseDTO;
import cn.ibdsr.human.util.RegUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: cdl
 * @email: iamcimon@qq.com
 * @date: 2021/8/3 13:35
 */
@Api(tags = "求职者控制器")
@RestController
@RequestMapping(value = "/user-details")
@Slf4j
@Validated
public class UserDetailController {
    @Resource
    private UserDetailService userDetailService;
    @Resource
    private LogFormatManager logFormatManager;
    @Resource
    private UserMapper userMapper;
    @Resource
    private JobService jobService;
    /**
     * cdl-ok
     * 获取个人信息
     */
    @ApiOperation("获取个人信息")
    @GetMapping("/information")
    @ResponseBody//

    public Result<UserDetailDTO> getUserDetail(@RequestAttribute(name = Const.USER_ID  ) Long userId) {
        logFormatManager.formatKeyPointLog("获取个人信息", "userId="+userId, BizConstants.MODULE_USER_WORKER);
        UserDetailDTO userDetailDTO=userDetailService.getUserDetail(userId);
        return new Result<>(userDetailDTO);
    }

    /**
     * cdl-ok
     * 保存个人信息
     */
    @ApiOperation("保存个人信息")
    @PutMapping
    @ResponseBody
    public Result<Long> save(@RequestAttribute(name = Const.USER_ID  ) Long userId,  @RequestBody @Valid UserDetailSaveDTO userDetailSaveDTO) {
        logFormatManager.formatKeyPointLog("获取个人信息", "userId="+userId+": "+JSONObject.toJSONString(userDetailSaveDTO), BizConstants.MODULE_USER_WORKER);
        if(!userDetailSaveDTO.getIdCardNo().matches(RegUtils.ID_CARD_NO)){
            throw new BusinessException(BizExceptionEnum.TOURIST_ID_CARD_NO_IS_ERROR);
        }
        Long flag=userDetailService.save(userId,userDetailSaveDTO);
        return new Result<>(flag);
    }


    /**
     * cdl-ok
     * 保存个人信息
     */
    @ApiOperation("设置工作状态")
    @PutMapping("/job-statuses")
    @ResponseBody
    public Result<Long> saveJobStatus(@RequestAttribute(name = Const.USER_ID  ) Long userId,  @RequestBody @Valid JobStatusDTO jobStatusDTO) {
        logFormatManager.formatKeyPointLog("设置工作状态", "userId="+userId+": "+JSONObject.toJSONString(jobStatusDTO), BizConstants.MODULE_USER_WORKER);
        Long flag=userDetailService.saveJobStatus(userId,jobStatusDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 获取进度
     */
    @ApiOperation("获取进度")
    @GetMapping("/progresses")
    @ResponseBody
    public Result<ServerInitDTO> getProgresses(@RequestAttribute(name = Const.USER_ID  ) Long userId ) {
        logFormatManager.formatKeyPointLog("获取进度", "userId="+userId, BizConstants.MODULE_USER_WORKER);
        ServerInitDTO serverInitDTO=userDetailService.getProgresses(userId);
        return new Result<>(serverInitDTO);
    }

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("分页获取岗位信息")
    @GetMapping("/jobs")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<JobCompanyResponseDTO> getUserJobList(@Valid JobQueryDTO jobQueryDTO  ) {
        logFormatManager.formatKeyPointLog("分页获取岗位信息",JSONObject.toJSONString(jobQueryDTO), BizConstants.MODULE_USER_WORKER);
        JobCompanyResponseDTO flag=jobService.getUserJobList( jobQueryDTO);
        return new Result<>(flag);
    }

}
